﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.IsolatedStorage;

using SharedGenomics.Core;

namespace SharedGenomics.Workbench.Annotations
{
    public class ActiveAnnotationsPersistanceManager
    {
        public virtual IEnumerable<KeyValuePair<AnnotationDescriptor, bool>> Load()
        {
            //get the available annotation descriptors and the activated annotation types
            var descriptors = this.AnnotationDescriptors;
            //return descriptors.Select(d => new KeyValuePair<AnnotationDescriptor, bool>(d, true));
            var activeAnnotationTypes = this.GetActiveAnnotationTypes(descriptors.Select(ad => ad.AnnotationType));

            //return the activation status of all annotation descriptors - if the annotation type is in the list of active annotations then the kvp value 
            //will be true, otherwise false
            //use a group join to emulate an outer join between the annotationDescriptors collection and the collection of active types
            return descriptors.GroupJoin(activeAnnotationTypes, ad => ad.AnnotationType, at => at, (ad, activeTypes) => new KeyValuePair<AnnotationDescriptor, bool>(ad, activeTypes.Count() > 0));
        }

        public virtual void Save(IEnumerable<Type> activeAnnotationTypes)
        {
            try
            {
                List<Type> annotationTypeList = (activeAnnotationTypes ?? Enumerable.Empty<Type>()).ToList();

                //active annotation types are saved by writing each active annotation type name to file
                IsolatedStorageFile isFile = IsolatedStorageFile.GetUserStoreForApplication();
                using (var preferencesFileStream = new IsolatedStorageFileStream("active_annotations.txt", System.IO.FileMode.OpenOrCreate, isFile))
                {
                    StreamWriter sw = new StreamWriter(preferencesFileStream);
                    for (int i = 0; i < annotationTypeList.Count - 1; i++)
                    {
                        sw.WriteLine(annotationTypeList[i].FullName);
                    }

                    //write last line
                    sw.Write(annotationTypeList.Last().FullName);
                }
            }
            catch (Exception)
            {
                //since this isn't that critical we can just swallow the exception here...
                //TODO: log exception? notify user? don't catch?
            }
        }

        protected virtual IEnumerable<AnnotationDescriptor> AnnotationDescriptors
        {
            get { return AnnotationDescriptor.AllAnnotationDescriptors; }
        }

        protected virtual IEnumerable<Type> GetActiveAnnotationTypes(IEnumerable<Type> availableAnnotationTypes)
        {
            return availableAnnotationTypes;
            //try
            //{
            //    IsolatedStorageFile isFile = IsolatedStorageFile.GetUserStoreForApplication();
            //    using (var preferencesFileStream = new IsolatedStorageFileStream("active_annotations.txt", System.IO.FileMode.OpenOrCreate, isFile))
            //    {
            //        TextFileReader reader = new TextFileReader(preferencesFileStream);
            //        return reader.Select(line => Type.GetType(line.Trim(), true));
            //    }
            //}
            //catch (Exception)
            //{
            //    //TODO: log exception? notify user?
            //    return availableAnnotationTypes.ToArray();
            //}
        }
    }
}
